package com.wisdom.smartbook.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;

import androidx.core.content.FileProvider;

import java.io.File;
import java.util.List;

/**
 * @ClassName: SystemUtil
 * @Description: java类作用描述
 * @Author: xjliu
 * @CreateDate: 2023/11/8 9:27 上午
 * @Version: 1.0
 */

public class SystemUtil {
    public static boolean isInstall(Context context, String packageName) {
        final PackageManager packageManager = context.getPackageManager();
        // 获取所有已安装程序的包信息
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
        for (int i = 0; i < pinfo.size(); i++) {
            if (pinfo.get(i).packageName.equalsIgnoreCase(packageName))
                return true;
        }
        return false;
    }


    /**
     * 获取文档类型，并打开
     *
     * @param f
     * @return
     */
    public static String getMIMEType(File f) {
        String type = "";
        String fName = f.getName();
        /* 取得扩展名 */
        String end = fName.substring(fName.lastIndexOf(".") + 1, fName.length()).toLowerCase();
        /* 依扩展名的类型决定MimeType */
        if (end.equals("pdf")) {
            type = "application/pdf";
        } else if (end.equals("m4a") || end.equals("mp3") || end.equals("mid") ||
                end.equals("xmf") || end.equals("ogg") || end.equals("wav")) {
            type = "audio/*";
        } else if (end.equals("3gp") || end.equals("mp4")) {
            type = "video/*";
        } else if (end.equals("jpg") || end.equals("gif") || end.equals("png") ||
                end.equals("jpeg") || end.equals("bmp")) {
            type = "image/*";
        } else if (end.equals("apk")) {
            type = "application/vnd.android.package-archive";
        } else if (end.equals("pptx") || end.equals("ppt")) {
            type = "application/vnd.ms-powerpoint";
        } else if (end.equals("docx") || end.equals("doc")) {
            type = "application/vnd.ms-word";
        } else if (end.equals("xlsx") || end.equals("xls")) {
            type = "application/vnd.ms-excel";
        } else {
            //如果无法直接打开，就跳出软件列表给用户选择
            type = "*/*";
        }
        return type;
    }
    /**
     * 获取打开文档意图
     *
     * @param file
     * @return
     */
    public static Intent getFileIntent(Activity activity, File file) {
        Uri uri = Uri.fromFile(file);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            //通过FileProvider创建一个content类型的Uri
            uri = FileProvider.getUriForFile(activity, "com.invt.fileprovider", file);
        }
        String type = getMIMEType(file);
        //优先使用WPS打开
        //检测是否安装了wps软件，没有安装选择默认打开
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(android.content.Intent.ACTION_VIEW);
        Bundle bundle = new Bundle();
        bundle.putString("OpenMode", "ReadOnly");// 只读模式
//        intent.setClassName("cn.wps.moffice_eng",
//                "cn.wps.moffice.documentmanager.PreStartActivity2");
//        intent.setDataAndType(uri,type);


        if (SystemUtil.isInstall(activity, "cn.wps.yun")) {
            intent.setClassName("cn.wps.yun",
                    "cn.wps.yun.applink.AppLinkActivity");
//                    "cn.wps.yun.ui.add.local.UploadLocalFileActivity");
//                    "cn.wps.yun.login.activity.LoginLandActivity");
            intent.setData(uri);
        } else {
            intent.addCategory("android.intent.category.DEFAULT");
            intent.setDataAndType(uri, type);
        }
        intent.putExtras(bundle);
        return intent;
    }




    /**
     * Android获取一个用于打开Excel文件的intent
     *
     * @param param
     * @return
     */
    public static Intent getExcelFileIntent(Context context, String param) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        File file = new File(param);
        String fileType = FileProviderHelper.getFileType(file);
        FileProviderHelper.setIntentDataAndType(context, intent
                , fileType, file, false);
        intent.setClassName("cn.wps.yun",
                "cn.wps.yun.applink.AppLinkActivity");
        return intent;
    }
}